<?php
function addScene($tbl, $keyword, $ename, $cname, $distance, $lat, $lon, $iso) {
	global $TBL_SCENE_SPOTS;
    $conn = connectDB();
	$sql  = "select * from  $TBL_SCENE_SPOTS where tbl='$tbl'";
	$result = mysql_query($sql, $conn);
	if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
		return "ERR_DB_ACCESS_ERROR";
	}
	if(mysql_num_rows($result) != 0) 
		return "ERR_SCENE_EXIST";

	$sql = "insert into $TBL_SCENE_SPOTS (tbl, keyword, ename, cname, distance, lat, lon, iso)".
			"values ('$tbl','$keyword','$ename','$cname',$distance,$lat,$lon,'$iso')";
	$result = mysql_query($sql, $conn);
	if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
		return "ERR_DB_ACCESS_ERROR";
	}
	else
		return "OK_OK";
}
function getAllScene() {
	global $TBL_SCENE_SPOTS;
	$conn = connectDB();
	$sql  = "select * from  $TBL_SCENE_SPOTS ORDER BY cname";
    $result = mysql_query($sql, $conn);
    if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
        return "ERR_DB_ACCESS_ERROR";
    }

	$idx = 0;
    while($row=mysql_fetch_row($result)){
        $scene[$idx]['tbl']         = $row[0];
        $scene[$idx]['keyword']     = $row[1];
        $scene[$idx]['ename']       = $row[2];
        $scene[$idx]['cname']       = $row[3];
        $scene[$idx]['distance']    = $row[4];
        $scene[$idx]['lat']    		= $row[5];
        $scene[$idx]['lon']    		= $row[6];
        $scene[$idx]['iso']    		= $row[7];
		$idx++;
    }
	return $scene;

}
function getScene($tbl) {
	global $TBL_SCENE_SPOTS;
	$conn = connectDB();
	$sql  = "select * from  $TBL_SCENE_SPOTS where tbl='$tbl'";
    $result = mysql_query($sql, $conn);
    if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
        return "ERR_DB_ACCESS_ERROR";
    }

	$idx = 0;
    while($row=mysql_fetch_row($result)){
        $scene['tbl']         = $row[0];
        $scene['keyword']     = $row[1];
        $scene['ename']       = $row[2];
        $scene['cname']       = $row[3];
        $scene['distance']    = $row[4];
        $scene['lat']    		= $row[5];
        $scene['lon']    		= $row[6];
        $scene['iso']    		= $row[7];
		$idx++;
    }
	return $scene;

}

function addHoliday($iso, $date, $text) {
	global $TBL_HOLIDAY;
	$conn = connectDB();
	$sql = "insert into $TBL_HOLIDAY(iso,date,text ) values ('$iso',$date,'$text')";
	$result = mysql_query($sql, $conn);
	if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
		return "ERR_DB_ACCESS_ERROR";
	}
	else
		return "OK_OK";
}
function isHoliday($iso, $date) {
	global $TBL_HOLIDAY;
	$conn = connectDB();
	$sql  = "select * from  $TBL_HOLIDAY where iso='$iso' and date = $date";
    $result = mysql_query($sql, $conn);
    if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
        return "ERR_DB_ACCESS_ERROR";
    }
	if(mysql_num_rows($result) == 0) 
		return false;
	else
		return true;
}

function create_tbl_record($tbl) {
	$conn = connectDB();
	$sql  = "create table $tbl"."_record ".
			"(tp BIGINT, PRIMARY KEY(tp), weather CHAR(32), temperature INT, checkins INT)";
    $result = mysql_query($sql, $conn);
    if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
        return "ERR_DB_ACCESS_ERROR";
    }
	return "OK_OK";
}
function insert_tbl_record($tbl, $tp, $weather, $temperature, $checkins) {
	$conn = connectDB();
	$sql  = "insert into $tbl"."_record ".
			"(tp,weather,temperature,checkins) values ($tp,'$weather','$temperature','$checkins')";
    $result = mysql_query($sql, $conn);
    if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
        return "ERR_DB_ACCESS_ERROR";
    }
	return "OK_OK";
}
function query_tbl_record($tbl, $tpMin, $tpMax, &$retval) {
	$conn = connectDB();
	$sql  = "select * from $tbl"."_record where tp >= $tpMin and tp <= $tpMax";
    $result = mysql_query($sql, $conn);
    if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
        $retval = "ERR_DB_ACCESS_ERROR";
		return null;
    }

	$idx = 0;
    while($row=mysql_fetch_row($result)){
        $record[$idx]['tp']         	= $row[0];
        $record[$idx]['weather']        = $row[1];
        $record[$idx]['temperature']    = $row[2];
        $record[$idx]['checkins']       = $row[3];
		$idx++;
    }
	if($idx == 0) {
		$retval = "OK_NODATA";
		return null;
	}
	else {
		$retval = "OK_OK";
		return $record;
	}
}
function insert_tbl_sample($tbl, $id, $checkins, $text, $ratio) {
	$conn = connectDB();
	$sql  = "insert into scene_samples ".
			"(tbl,id,checkins,text,ratio) values ('$tbl',$id,$checkins,'$text',$ratio)";
    $result = mysql_query($sql, $conn);
    if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
        return "ERR_DB_ACCESS_ERROR";
    }
	return "OK_OK";
}
function query_tbl_sample($tbl) {
	$conn = connectDB();
	$sql  = "select * from scene_samples where tbl='$tbl'";
    $result = mysql_query($sql, $conn);
    if(!$result) {
        echo "ErrNo: ". mysql_errno()." Desc:". mysql_error();
        return "ERR_DB_ACCESS_ERROR";
    }

	$idx = 0;
    while($row=mysql_fetch_row($result)){
        $sample[$idx]['tbl']         	= $row[0];
        $sample[$idx]['id']         	= $row[1];
        $sample[$idx]['checkins']       = $row[2];
        $sample[$idx]['text']       	= $row[3];
        $sample[$idx]['ratio']         	= $row[4];
		$idx++;
    }
	return $sample;
}
#insert_tbl_sample('abc', 123,456,'hello',3.12);
#$a=query_tbl_sample('abc');
#echo "\n";
#foreach( $a as $key => $val) {
#	echo " ======== $key\n";
#	echo $val['tbl']."\n";
#	echo $val['id']."\n";
#	echo $val['checkins']."\n";
#	echo $val['text']."\n";
#	echo $val['ratio']."\n";
#}
?>
